/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.easoftware.code.beans;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.easoftware.code.beans.cargas.OrdenCA;
import net.easoftware.code.error.EaError;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.BigDecimalConverter;
import org.apache.commons.beanutils.converters.BigIntegerConverter;
import org.apache.commons.beanutils.converters.DateConverter;

/**
 *
 * @author waumovil-javaengineer
 */
public abstract class JpaEntityConverter {

    static {
        ConvertUtils.register(new DateConverter(null), Date.class);
        ConvertUtils.register(new BigDecimalConverter(null), BigDecimal.class);
        ConvertUtils.register(new BigIntegerConverter(null), BigInteger.class);
    }
    private Class destClassType;

    public Class getDestClassType() {
        return destClassType;
    }

    public void setDestClassType(Class destClassType) {
        this.destClassType = destClassType;
    }

    public Object getEntityBean() throws EaError {
        try {
            Object dest = destClassType.newInstance();

            BeanUtils.copyProperties(dest, this);
            return dest;
        }
        catch (InstantiationException ex) {
            Logger.getLogger(JpaEntityConverter.class.getName()).log(Level.SEVERE, null, ex);
            throw new EaError("error generando entidad. Error de instanciacion");
        }
        catch (IllegalAccessException ex) {
            Logger.getLogger(OrdenCA.class.getName()).log(Level.SEVERE, null, ex);
            throw new EaError("error generando entidad. Acceso ilegal de atritubos");
        }
        catch (InvocationTargetException ex) {
            Logger.getLogger(OrdenCA.class.getName()).log(Level.SEVERE, null, ex);
            throw new EaError("error generando entidad. Tipo de invocacion");
        }
    }
}
